package com.ian.domain.dto;


import com.ian.enums.ApiStatusEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * Restful api请求结果
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ApiResult<T> implements Serializable {


    private T result;


    private String message;


    private Integer code;

    /**
     * 描述: 成功
     *
     * @param code  code
     * @param message  message
     * @param data  data
     * @return {@link ApiResult<T>}
     * @author wenxudong
     * @date 2021/12/24 11:51
     */
    public static <T> ApiResult<T> success(int code, String message, T data) {
        ApiResult<T> result = new ApiResult<T>();
        result.setMessage(message);
        result.setCode(code);
        result.setResult(data);
        return result;
    }

    /**
     * 描述: 成功
     *
     * @param code  code
     * @param message  message
     * @return {@link ApiResult<T>}
     * @author wenxudong
     * @date 2021/12/24 11:49
     */
    public static <T> ApiResult<T> success(int code, String message) {
        ApiResult<T> result = new ApiResult<T>();
        result.setMessage(message);
        result.setCode(code);
        return result;
    }

    /**
     * 描述: 编码设置
     *
     * @param code  code
     * @return {@link ApiResult<T>}
     * @author wenxudong
     * @date 2021/12/30 9:06
     */
    public static <T> ApiResult<T> success(int code) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(code);
        return result;
    }

    /**
     * 成功
     *
     * @param data - 业务数据
     * @return ApiResult
     */
    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = new ApiResult<T>();
        result.setResult(data);
        result.setCode(ApiStatusEnum.Success.getCode());
        result.setMessage(ApiStatusEnum.Success.getMessage());
        return result;
    }

    /**
     * 描述: 成功
     *
     * @param statusEnum  statusEnum
     * @return {@link ApiResult<T>}
     * @author wenxudong
     * @date 2021/12/24 13:48
     */
    public static <T> ApiResult<T> success(ApiStatusEnum statusEnum) {
        ApiResult<T> result = new ApiResult<T>();
        result.setMessage(statusEnum.getMessage());
        result.setCode(statusEnum.getCode());
        return result;
    }


    /**
     * 成功
     */
    public static <T> ApiResult<T> success() {
        ApiResult<T> result = new ApiResult<T>();
        result.setResult(null);
        result.setCode(ApiStatusEnum.Success.getCode());
        result.setMessage(ApiStatusEnum.Success.getMessage());
        return result;
    }

    /**
     * 描述: 错误结果
     *
     * @param errorCode  错误码
     * @param errorMessage  错误信息
     * @param data  错误数据
     * @return {@link ApiResult<T>}
     * @author wenxudong
     * @date 2021/12/23 9:14
     */
    public static <T> ApiResult<T> error(int errorCode, String errorMessage, T data) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(errorCode);
        result.setMessage(errorMessage);
        result.setResult(data);
        return result;
    }

    /**
     * 错误结果
     *
     * @param errorCode    - 错误码
     * @param errorMessage - 错误信息
     * @return ApiResult
     */
    public static <T> ApiResult<T> error(int errorCode, String errorMessage) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(errorCode);
        result.setMessage(errorMessage);
        return result;
    }

    /**
     * 错误结果
     *
     * @param errorMessage - 错误信息
     * @return ApiResult
     */
    public static <T> ApiResult<T> error(String errorMessage) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(ApiStatusEnum.Error.getCode());
        result.setMessage(errorMessage);
        return result;
    }

    /**
     * 描述: 编码设置
     *
     * @param code  code
     * @return {@link ApiResult<T>}
     * @author wenxudong
     * @date 2021/12/30 9:06
     */
    public static <T> ApiResult<T> error(int code) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(code);
        return result;
    }

    /**
     * 错误结果
     */
    public static <T> ApiResult<T> error() {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(ApiStatusEnum.Error.getCode());
        result.setMessage(ApiStatusEnum.Error.getMessage());
        return result;
    }

}